package weaver.interfaces.jhthome.duss.action;

import com.weaver.general.BaseBean;
import com.weaver.general.Util;
import weaver.conn.RecordSet;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

public class WeaWlxjdAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        writeLog(">>>>>WeaCgsqlcAction>>>>>requestid=" + requestid);
        writeLog(">>>>>WeaCgsqlcAction>>>>>tablename=" + tablename);
        JhthomeUtil jhthomeUtil = new JhthomeUtil();
        try {
            rs.execute("select * from " + tablename + " where requestid=" + requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));
            String sqrq = Util.null2String(rs.getString("sqrq"));
            String csr = Util.null2String(rs.getString("csr"));
            String bt = Util.null2String(rs.getString("bt"));
            ResourceComInfo resourceComInfo = new ResourceComInfo();
            String workflowId = "1326";
            String isNextFlow = "1";
            String newTablename = "formtable_main_356";
            int count = 0;
            String bjrs = jhthomeUtil.getBjrByDtl2(tablename, mainid);
            String[] arr = bjrs.split(",");
            int counts = arr.length;
            for (String ryid : arr) {
                Map<String, String> requestMainMap = new HashMap<String, String>();
                requestMainMap.put("sqr", ryid);
                requestMainMap.put("sqrq", sqrq);
                requestMainMap.put("sfjsbj", "1");
                requestMainMap.put("bm", resourceComInfo.getDepartmentID(ryid));
                requestMainMap.put("gllc", requestid);
                requestMainMap.put("csr", csr);
                requestMainMap.put("bjcs", "0");
                String newRequestid = jhthomeUtil.WorkflowCreateByRequestMap(ryid, workflowId, bt, isNextFlow, requestMainMap);
                if (Util.getIntValue(newRequestid) > 0) {
                    rs.execute("select * from " + tablename + "_dt2 where mainid=" + mainid + " and bjr=" + ryid);
                    while (rs.next()) {
                        if (Util.getIntValue(newRequestid) > 0) {
                            Map<String, Object> requestDt1Map = new HashMap<String, Object>();
                            String newMainid = jhthomeUtil.getIdByRequestId(newTablename, newRequestid);
                            requestDt1Map.put("mainid", newMainid);
                            requestDt1Map.put("bh", Util.null2String(rs.getString("bh")));
                            requestDt1Map.put("wlmcxxgg", Util.null2String(rs.getString("wlmcxxgg")));
                            requestDt1Map.put("bcsm", Util.null2String(rs.getString("bcsm")));
                            requestDt1Map.put("sliang", Util.null2String(rs.getString("sliang")));
                            requestDt1Map.put("dwmm", Util.null2String(rs.getString("dwmm")));
                            requestDt1Map.put("djjmf", Util.null2String(rs.getString("djjmf")));
                            requestDt1Map.put("pcbz", Util.null2String(rs.getString("pcbz")));
                            requestDt1Map.put("jgydpl", Util.null2String(rs.getString("jgydpl")));
                            requestDt1Map.put("sfecjg", Util.null2String(rs.getString("sfecjg")));
                            requestDt1Map.put("yxq", Util.null2String(rs.getString("yxq")));
                            requestDt1Map.put("zzcp", Util.null2String(rs.getString("zzcp")));
                            requestDt1Map.put("kh", Util.null2String(rs.getString("kh")));
                            requestDt1Map.put("sl", Util.null2String(rs.getString("sl")));
                            requestDt1Map.put("ylzssm", Util.null2String(rs.getString("ylzssm")));
                            requestDt1Map.put("qmqq", Util.null2String(rs.getString("qmqq")));
                            requestDt1Map.put("ssl", Util.null2String(rs.getString("ssl")));
                            requestDt1Map.put("bcsx", Util.null2String(rs.getString("bcsx")));
                            requestDt1Map.put("fjsc", Util.null2String(rs.getString("fjsc")));
                            requestDt1Map.put("bjr", Util.null2String(rs.getString("bjr")));
                            requestDt1Map.put("sbje", Util.null2String(rs.getString("sbje")));
                            requestDt1Map.put("sbr", Util.null2String(rs.getString("sbr")));
                            jhthomeUtil.updateDetailData(newTablename + "_dt1", requestDt1Map);
                        }
                    }
                    count++;
                }
            }

            if (counts == count) {
                return Action.SUCCESS;
            } else {
                requestInfo.getRequestManager().setMessageid("111111");
                requestInfo.getRequestManager().setMessagecontent("Action Exception:子流程创建失败!");
                return Action.FAILURE_AND_CONTINUE;
            }
        } catch (Exception e) {
            e.printStackTrace();
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action Exception!");
            return Action.FAILURE_AND_CONTINUE;
        }

    }


}
